#!/usr/bin/env python
#
#   Copyright (C) 2006 Jonathan Saggau                                    
#   saggau@gmail.com                                                      
#                                                                         
#   This program is free software; you can redistribute it and/or modify  
#   it under the terms of the GNU General Public License as published by  
#   the Free Software Foundation; either version 2 of the License, or     
#   (at your option) any later version.                                   
#                                                                         
#   This program is distributed in the hope that it will be useful,       
#   but WITHOUT ANY WARRANTY; without even the implied warranty of        
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         
#   GNU General Public License for more details.                          
#                                                                         
#   You should have received a copy of the GNU General Public License     
#   along with this program; if not, write to the                         
#   Free Software Foundation, Inc.,                                       
#   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.  
#
import msg
import decoder
import server

#Buffer Commands
def alloc(id, frames, channels, completionmsg = None):
    return msg.msg('/b_alloc', id, frames, channels, 
                completionmsg = completionmsg)
    
def allocRead(id, path, startframe = 0, readframes = 0,
                     completionmsg = None):
    return msg.msg('/b_allocRead', id, path, startframe, readframes, 
                completionmsg = completionmsg)
    
def read(id, path, startframeinfile = 0, readframes = -1, 
                startframebuffer = 0, leaveopen = 0, completionmsg = None):
    return msg.msg('/b_read', id, path, startframefile, readframes, 
                startframebuffer, leaveopen, completionmsg = completionmsg)
    
def write(id, path, headerformat, sampleformat, writeframes = -1, 
                 startframebuffer = 0, leaveopen = 0,
                 completionmsg = None):
    return msg.msg('b_write', id, path, headerformat, sampleformat, 
                   writeframes, startframebuffer, leaveopen, 
                   completionmsg = completionmsg)
    
def free(id, completionmsg = None):
    return msg.msg('/b_free', id, completionmsg = completionmsg)
    
def zero(id, completionmsg = None):
    return msg.msg('/b_zero', completionmsg = completionmsg)
    
def set(id, indexvaluedict):
    return msg.dictmsg(id, indexvaluedict)

def setn(id, indexValuesDict):
    return msg.setnmsg('/b_setn', indexValuesDict)
    
def fill(id, indexNumValueDict):
    """indexNumsValueDict = index: [number_of_values_to_fill, value]"""
    return msg.dictmsg(["/b_fill", id], indexNumValueDict)
    
# def gen(*args):
#     pass
    
def close(id):
    return msg.msg('/b_close', id)
    
def query(*ids):
    return msg.msg('/b_query', ids)
    
def get(id, indices):
    return msg.msg('/b_get', id, indices)
    #indices can be a single value or a list/tuple of values
    
def getn(id, indexNumberOfSamplesDict):
    return msg.dictmsg('/b_getn', id, indexNumberOfSamplesDict)
    
class Buffer(object):
    def __init__(self, controller, ID, filePath):
        self.ID = ID
        self.filePath = filePath
        self.controller = controller
        self.decoder = decoder.Decoder()
        
    def _sendMsg(self, msg, incoming = None):
        if self.filePath and self.ID:
            self.controller.listSendMsg(msg)
            if incoming:
                response = self.controller.receive(incoming)
                return self.decoder.decode(response)
            else:
                return {}
        else:
            return {}
    
    def query(self):
        response = self._sendMsg(query(self.ID), '/b_info')
        return response
        
    def allocRead(self, startframe = 0, readframes = 0,
                     completionmsg = None):
        return self._sendMsg(allocRead(self.ID, self.filePath,\
                     startframe, readframes, completionmsg),\
                     '/done')
                     
    def close(self):
        return self._sendMsg(close(self.ID))
                                
    def __del__(self):
        self.close()